#导入数据库
from db import db1





class ACL:
    def __init__(self):
        pass
    #给用户添加权限
    def set_resource(self,uid,reslist):
        for i in reslist:
            sql="insert into user_reso(uid,rid) values(%d,%d)%(int(uid),int(i))"
            db1.insert(sql)
    #修改权限
    def update_resource(self,uid,reslist):
        #先删除原来的再重新添加
        sql="delete from user_reso where uid=%d"%(int(uid))
        db1.insert(sql)
        self.set_resource(uid,reslist)
    #删除某个权限
    def delete_resource(self,uid,reourseid):
        sql="delete from user_reso where uid=%d and rid=%d"%(int('uid'),int('rid'))
        db1.insert(sql)
    #获取页面资源列表
    def get_page_reslist(self,uid):
        sql="select r.url from user_reso as u inner join resource as r on u.rid=r.id where r.type=1 and u.uid=%d"%(int(uid))
        res=db1.findall(sql)
        list = []
        for i in res:
            list.append(i['url'])
        return list
    #获取接口资源列表
    def get_interface_reslist(self,uid):
        sql="select r.url as url from user_reso as u inner join resource as r on u.rid=r.id where r.type=2 and u.uid=%d"%(int(uid))
        res=db1.findall(sql)
        list=[]
        for i in res:
            list.append(i['url'])
        return list

acl=ACL()